﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AkazoNobel.Scheduler.Core;
using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service;
using AkazoNobel.Ultimus.Extension;
using Dapper;
using Quartz;
using Ultimus.WFServer;
using System.Configuration;
using AkazoNobel.Scheduler.StoreMasterData.Common;

namespace AkazoNobel.Scheduler.StoreMasterData.Tasks
{
    public class ToSendEmail : BaseTask
    {
        public override void ExecuteTask(IJobExecutionContext context)
        {
          
            LogDebug(DateTime.Now.ToString());
            LogDebug("==== 开始发送设备转库存邮件 ====1");

            using (var connection = SqlConnectionHelper.GetConnection(Constant.MDC_DB_NAME))
            {
                var count = connection.ExecuteScalar(string.Format(@"select count(*) from AkzoNobelSMD..v_DeviceDeposit a,t_smd_Relation b,t_Assessment c,t_StoreDataChangeLog d
                                                                       where a.FullSetSN=b.FullsetSN and b.BusinessKey=c.BusinessKey and b.BusinessProcess=c.BusinessProcess and c.ID=d.AssessmentID  and d.AfterValue='经销商处寄存'"));
                LogDebug(string.Format("有{0}台设备暂存需要处理2", count));
                string link = ConfigurationManager.AppSettings["ToLink"];


                string sql = string.Format("select * from AkzoNobelSMD..UserEmail where Mtype = '{0}' and MStatus = 0", Convert.ToInt32(PublicEnum.MType.TemporaryEquipment).ToString());
                var rows = connection.Query(sql).ToList(); //获得所有邮箱              
                //LogDebug(rows.Count.ToString());
                //LogDebug(rows.ToString());
                foreach (var row in rows)
                {
                    string email = row.Email;
                    //LogDebug(email);
                    if (!email.Contains("@"))
                    {
                        continue;
                    }

                    var variables = new List<Variable>();
                    string applicationNo;

                    //发起流程
                    variables.Add("Recipient", email);//email
                    variables.Add("TodoCount", count);//count
                    variables.Add("ToLink", link);//link
                    variables.Add("Date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    //LogDebug("===2");

                    LogDebug(string.Format("Recipient:'{0}' @TodoCount:'{1}' @ToLink:'{2}'  @Date:'{3}'",
                        email.Trim(), count, link, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    //LogDebug("====3");
                    TaskHelper.NewIncident("d30.intra/svc_dpcna_mdc", "Todo Email", "SMD设备暂存待处理提醒", variables,
                        out applicationNo);
                    LogDebug("==== 流程编号" + applicationNo + " ====3");
                    // 发布流程之后，线程等待1秒
                    System.Threading.Thread.Sleep(1000);
                }

                #region 测试 y_2016_11_14

                //                var count = connection.ExecuteScalar(@"select count(*) from AkzoNobelSMD..v_DeviceDeposit a,ultimusmdc3.dbo.t_smd_Relation b,UltimusMDC3..t_Assessment c,UltimusMDC3..t_StoreDataChangeLog d
                //                                                       where a.FullSetSN=b.FullsetSN and b.BusinessKey=c.BusinessKey and b.BusinessProcess=c.BusinessProcess and c.ID=d.AssessmentID ");
                //                LogDebug(string.Format("有{0}台设备暂存需要处理2", count));
                //                string link = "www.baidu.com";
                //                    var variables = new List<Variable>();
                //                    string applicationNo;
                //                    //发起流程
                //                    variables.Add("ToEmail", "1748498231@qq.com");//email
                //                    variables.Add("ToCount", count);//count
                //                    variables.Add("ToLink", "www.baidu.com");//link
                //                    variables.Add("DateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                //                    TaskHelper.NewIncident("d30.intra/svc_dpcna_mdc", "Todo Remind", "SMD设备暂存待处理提醒", variables,
                //                        out applicationNo);
                //                    LogDebug("==== 流程编号" + applicationNo + " ====3");

                //                    // 发布流程之后，线程等待1秒
                //                    System.Threading.Thread.Sleep(1000);

                #endregion

               
            }
        }

        private void LogDebug(string content)
        {
            Console.WriteLine(content);
            Logger.Debug(content);
        }
    }
}
